from django.conf.urls.defaults import patterns, include, url
from django.conf import settings
from django.contrib import admin
from django.contrib.sites.models import Site

from zinnia.settings import PROTOCOL
from zinnia.settings import COPYRIGHT
from zinnia.settings import FEEDS_FORMAT
from zinnia.sitemaps import TagSitemap
from zinnia.sitemaps import EntrySitemap
from zinnia.sitemaps import CategorySitemap

from fragments.views import FragmentCreate, FragmentDetail, FragmentList

sitemaps = {'tags': TagSitemap,
            'blog': EntrySitemap,
            'categories': CategorySitemap,}

extra_context = {
    'protocol': PROTOCOL,
    'site': Site.objects.get_current()
}
extra_context_opensearch = extra_context.copy()
extra_context_opensearch.update({
    'copyright': COPYRIGHT,
    'feeds_format': FEEDS_FORMAT
})

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/filebrowser/', include('filebrowser.urls')),
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^grappelli/', include('grappelli.urls')),
    url(r'^search/', include('zinnia.urls.search')),
    url(r'^sitemap/', include('zinnia.urls.sitemap')),
    url(r'^trackback/', include('zinnia.urls.trackback')),
    url(r'^blog/tags/', include('zinnia.urls.tags')),
    url(r'^blog/feeds/', include('zinnia.urls.feeds')),
    url(r'^blog/categories/', include('zinnia.urls.categories')),
    url(r'^paste/$', FragmentList.as_view(), name="paste_fragment_list"),
    url(r'^paste/create/', FragmentCreate.as_view(), name="paste_fragment_create"),
    url(r'^paste/(?P<pk>[0-9]+)/', FragmentDetail.as_view(), name="paste_fragment_detail"),
    url(r'^comments/', include('django.contrib.comments.urls')),
    url(r'^', include('zinnia.urls.quick_entry')),
    url(r'^', include('zinnia.urls.entries')),
)

urlpatterns += patterns('django.contrib.sitemaps.views',
                        url(r'^sitemap.xml$', 'index',
                            {'sitemaps': sitemaps}),
                        url(r'^sitemap-(?P<section>.+)\.xml$', 'sitemap',
                            {'sitemaps': sitemaps}),)

urlpatterns += patterns('django.views.generic.simple',
    url(r'^rsd.xml$', 'direct_to_template',
        {'template': 'zinnia/rsd.xml',
         'mimetype': 'application/rsd+xml',
         'extra_context': extra_context}, name='zinnia_rsd'),
    url(r'^wlwmanifest.xml$', 'direct_to_template',
        {'template': 'zinnia/wlwmanifest.xml',
         'mimetype': 'application/wlwmanifest+xml',
         'extra_context': extra_context}, name='zinnia_wlwmanifest'),
    url(r'^opensearch.xml$', 'direct_to_template',
        {'template': 'zinnia/opensearch.xml',
         'mimetype': 'application/opensearchdescription+xml',
         'extra_context': extra_context_opensearch}, name='zinnia_opensearch'),
)

if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
   )
